package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.List;

import com.openbravo.bean.erp.viewbean.ProductCategoryTransaction;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.StaticSentence;
import cn.ekuma.data.dao.ViewDAO;

public class ProductCategoryTransactionDAO extends
		ViewDAO<ProductCategoryTransaction> {

	public ProductCategoryTransactionDAO(I_Session s) {
		super(s);
	}

	@Override
	public ProductCategoryTransaction readValues(DataRead dr,ProductCategoryTransaction p)
			throws BasicException {
		if(p==null)
			p=new ProductCategoryTransaction();
        p.setProductCategoryId(dr.getString(1));
        p.orderTypeName=dr.getString(2);
        p.orderType=dr.getInt(3);
        p.multiply=dr.getDouble(4);
        p.total=dr.getDouble(5);
        return p;
	}
	
	 public List<ProductCategoryTransaction> productCategoryTransactionListQBF(Object filter) throws BasicException{
	        return new StaticSentence(s, new QBFBuilder(
	                "SELECT  C.ID,E.NAME, T.ORDERTYPE, SUM(L.UNITS), SUM(L.UNITS*L.PRICE)  "
	                + "FROM ORDERLINES L LEFT  JOIN ORDERS T ON L.ORDERRID = T.ID "
	                + "LEFT  JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID "
	                + "LEFT JOIN PRODUCTS PS ON L.PRODUCT = PS.ID "
	                + "LEFT JOIN CATEGORIES C ON PS.CATEGORY = C.ID "
	                + "LEFT OUTER JOIN OPERATIONPRODUCT OP ON L.PRODUCT = OP.PRODUCT  "
	                + "LEFT OUTER JOIN OPERATIONPRODUCTCATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
	                + "WHERE L.PRODUCT IS NOT NULL AND ?(QBF_FILTER)  "
	                + "GROUP BY C.ID , E.NAME, T.ORDERTYPE  ",
	                new String[]{"T.CURDATE", "T.CURDATE", "T.CUSTOMER", "T.LOCATIONREF","OPC.ID"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}), this).list(filter);
	    }

	    public  List<ProductCategoryTransaction> productCategoryPOSTransactionListQBF(Object filter) throws BasicException {
	        return new StaticSentence(s, new QBFBuilder(
	                "SELECT  C.ID , T.TICKETTYPE, SUM(L.UNITS), SUM(L.UNITS*L.PRICE)  "
	                + "FROM TICKETLINES L LEFT JOIN TICKETS T ON L.TICKET = T.ID "
	                + "LEFT  JOIN PRODUCTS PS ON L.PRODUCT = PS.ID "
	                + "LEFT  JOIN CATEGORIES C ON PS.CATEGORY = C.ID "
	                + "LEFT OUTER JOIN OPERATIONPRODUCT OP ON L.PRODUCT = OP.PRODUCT  "
	                + "LEFT OUTER JOIN OPERATIONPRODUCTCATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
	                + "WHERE L.PRODUCT IS NOT NULL AND ?(QBF_FILTER) "
	                + "GROUP BY C.ID, T.TICKETTYPE ", 
	                new String[]{"T.CURDATE", "T.CURDATE", "T.CUSTOMER", "T.LOCATIONREF","OPC.ID"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}), new PosTransactionRead()).list(filter);
	    }

	    @Override
	    public Class getSuportClass() {
	        return ProductCategoryTransaction.class;
	    }

	    public static class PosTransactionRead implements SerializerRead<ProductCategoryTransaction>{
			@Override
			public ProductCategoryTransaction readValues(DataRead dr) throws BasicException {
				ProductCategoryTransaction p=new ProductCategoryTransaction();
		        p.setProductCategoryId(dr.getString(1));
		        p.orderTypeName="POS";
		        p.orderType=dr.getInt(2);
		        p.multiply=dr.getDouble(3);
		        p.total=dr.getDouble(4);
		        return p;
			}
	    }
}
